package com.caishi.lkx.user.api;


import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.caishi.lkx.user.model.DistributionUser;
import com.caishi.lkx.user.service.IDistributionUserService;
import com.zzw.common.annotation.ApiResult;
import org.springframework.web.bind.annotation.*;


import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user/distributionUser")
public class DistributionUserController {
    @Resource
    private IDistributionUserService distributionUserService;


    @ApiResult
    @PostMapping("/disabled")
    public Boolean  disabledAgent(@RequestBody Map<String, Object> payload) {
        String agentId = (String) payload.get("agentId");
        Boolean deleted = (Boolean) payload.get("deleted");
//        默认传入deleted为true
         return distributionUserService.disabledAgent(agentId, deleted == null);
    }
//    @GetMapping
//    public List<DistributionUser> getAll() {
//        return distributionUserService.list();
//    }

    @GetMapping("/{id}")
    public DistributionUser getById(@PathVariable String id) {
        return distributionUserService.getById(id);
    }

    @PostMapping
    public void create(@RequestBody DistributionUser user) {
        distributionUserService.save(user);
    }

    @PutMapping
    public void update(@RequestBody DistributionUser user) {
        distributionUserService.updateById(user);
    }

    @ApiResult
    @DeleteMapping
    public void delete(@RequestBody Map<String,Object> payload) {
        String id =(String) payload.get("id");
        distributionUserService.update(null,  new LambdaUpdateWrapper<DistributionUser>().set(DistributionUser::getDeleted, true).eq(DistributionUser::getId, id));
//        distributionUserService.removeById(id);
    }
    @ApiResult
    @GetMapping("/tree")
    public Page<DistributionUser> getDistributionTree(@RequestParam(required = false,defaultValue = "1") Integer current,  @RequestParam(required = false,defaultValue = "10") Integer size) {
        Page<DistributionUser> page = new Page<>(current, size);
        Page<DistributionUser> distributionTop = distributionUserService.getDistributionTop(page);
        List<DistributionUser> distributionUserTree = distributionUserService.getDistributionUserTree(distributionTop.getRecords().stream().map(DistributionUser::getId).toList());
        distributionTop.setRecords(distributionUserTree);
        return distributionTop;
    }
}
